﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class ZiCaiBreadCrumb2 : Literal
    {
        protected Queue crumbs = new Queue();
        protected HtmlAnchor anchor;
        private int zicaiSecondLevelCategoryID = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ZicaiSecondLevelCategoryID);
        private int zicaiTopLevelCategoryID = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ZicaiTopLevelCategoryID);
        private string greenNum = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.GreenNum);
        private int kind = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Kind);
        private string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);

        protected override void CreateChildControls()
        {
            crumbs.Enqueue(GetAnchor("本企业所有资材大类", GetUserZiCaiList2Url(0,0,0)));

            if (zicaiSecondLevelCategoryID > 0)
            {
                ZiCai secondZiCai = BusinessManager.GetZiCai(zicaiSecondLevelCategoryID);
                if (secondZiCai != null)
                {
                    if (secondZiCai.UpZiCaiId.Value > 0)
                    {
                        ZiCai firstZiCai = BusinessManager.GetZiCai(secondZiCai.UpZiCaiId.Value);
                        if (firstZiCai != null)
                        {
                            crumbs.Enqueue(GetAnchor(firstZiCai.Title.Value, GetUserZiCaiList2Url(firstZiCai.EntityId.Value, 0, 0)));
                        }
                    }
                    crumbs.Enqueue(GetAnchor(secondZiCai.Title.Value, GetUserZiCaiList2Url(secondZiCai.UpZiCaiId.Value, secondZiCai.EntityId.Value, 0)));
                }
            }
            else if (zicaiTopLevelCategoryID > 0)
            {
                ZiCai firstZiCai = BusinessManager.GetZiCai(zicaiTopLevelCategoryID);
                if (firstZiCai != null)
                {
                    crumbs.Enqueue(GetAnchor(firstZiCai.Title.Value, GetUserZiCaiList2Url(firstZiCai.EntityId.Value, 0, 0)));
                }
            }
        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("资材类别导航:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(ResourceManager.GetString("BreadCrumb_Seperator"));
                }
            }

        }

        private string GetUserZiCaiList2Url(int topCategoryId,int secondCategoryId,int thirdCategoryId)
        {
            string str= string.Empty;
            if (kind == 1)
            {
                if (tradeType == "0")
                {
                    str = SiteUrls.Instance().GetCompanyMachineChuShouList2Url(greenNum, topCategoryId, secondCategoryId);
                }
                else if (tradeType == "1")
                {
                    str = SiteUrls.Instance().GetCompanyMachineQiuGouList2Url(greenNum, topCategoryId, secondCategoryId);
                }
            }
            else if (kind == 2)
            {
                if (tradeType == "0")
                {
                    str = SiteUrls.Instance().GetCompanyFacilityChuShouList2Url(greenNum, topCategoryId, secondCategoryId);
                }
                else if (tradeType == "1")
                {
                    str = SiteUrls.Instance().GetCompanyFacilityQiuGouList2Url(greenNum, topCategoryId, secondCategoryId);
                }
            }
            else if (kind == 3)
            {
                if (tradeType == "0")
                {
                    str = SiteUrls.Instance().GetCompanyAgriculturalChuShouList2Url(greenNum, topCategoryId, secondCategoryId);
                }
                else if (tradeType == "1")
                {
                    str = SiteUrls.Instance().GetCompanyAgriculturalQiuGouList2Url(greenNum, topCategoryId, secondCategoryId);
                }
            }

            return str;
        }
    }
}